package com.example.his.api.mis.service.impl;

import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.RuleDao;
import com.example.his.api.db.pojo.RuleEntiy;
import com.example.his.api.mis.service.RuleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Service
public class RuleServiceImpl implements RuleService {
  
  @Resource
  private RuleDao ruleDao;
  
  @Override
  public ArrayList <HashMap> searchAllRule() {
    return ruleDao.searchAllRule();
  }
  
  @Override
  public PageUtils searchByPage(Map param) {
    ArrayList <HashMap> list = new ArrayList <>();
    long count = ruleDao.searchCount(param);
    if(count > 0) {
      list = ruleDao.searchByPage(param);
    }
    int pageIndex = MapUtil.getInt(param, "page");
    int pageSize = MapUtil.getInt(param, "length");
    PageUtils pageUtils = new PageUtils(list, count, pageIndex, pageSize);
    return pageUtils;
  }
  
  @Override
  @Transactional
  public boolean insert(RuleEntiy ruleEntiy) {
    int rows = ruleDao.insert(ruleEntiy);
    return rows == 1;
  }
  
  @Override
  public HashMap searchById(int id) {
    HashMap res = ruleDao.searchById(id);
    return res;
  }
  
  @Override
  public boolean update(RuleEntiy ruleEntiy) {
    int rows = ruleDao.update(ruleEntiy);
    return rows == 1;
  }
  
  @Override
  @Transactional
  public boolean deleteById(int id) {
    int rows = ruleDao.deleteById(id);
    return rows == 1;
  }
}
